package com.example.yiwei.crm.view.Class.recycleviewadapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.example.yiwei.crm.R;
import com.example.yiwei.crm.entity.UseCareEntity;

import java.util.List;

/**
 * Created by Administrator on 2018/1/9.
 */

public class UseCarRecycleAdapter extends RecyclerView.Adapter<UseCarRecycleAdapter.MyViewHolder> implements View.OnClickListener{

    private List<UseCareEntity> list;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    public UseCarRecycleAdapter(Context context, List<UseCareEntity> bean){
        this.list = bean;
        this.context = context;
        Log.d("aa",list.toString());
    }
    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder;
        View view;
//        if ("hospital".equals(type)){
            view= LayoutInflater.from(
                    context).inflate(R.layout.layout_simple_use_car, parent,
                    false);
//        }else{
//            view= LayoutInflater.from(
//                    context).inflate(R.layout.layout_item_select_office_recycleview, parent,
//                    false);
//        }
        holder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.text_reason.setText(list.get(position).getReason());
        holder.text_time.setText(list.get(position).getTime());
        holder.text_address.setText(list.get(position).getAddress());
        if ("0".equals(list.get(position).getState())){
            holder.text_state.setTextColor(Color.BLACK);
            holder.text_state.setText("未审批");
        }else if ("1".equals(list.get(position).getState())){
            holder.text_state.setTextColor(Color.GREEN);
            holder.text_state.setText("审核通过");
        }else if ("2".equals(list.get(position).getState())){
            holder.text_state.setTextColor(Color.RED);
            holder.text_state.setText("不予通过");
        }
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView text_reason,text_time,text_address,text_state;

        public MyViewHolder(View view)
        {
            super(view);
            text_reason = (TextView) view.findViewById(R.id.text_reason);
            text_time = (TextView) view.findViewById(R.id.text_time);
            text_address = (TextView) view.findViewById(R.id.text_address);
            text_state = (TextView) view.findViewById(R.id.text_state);
        }
    }
}
